<html>
<head>
    <meta charset="UTF-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css"/>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<!--<div class="demoTable">
    员工编号：
    <div class="layui-inline">
        <input class="layui-input" name="auserId" id="auserId" autocomplete="off">
    </div>
    员工姓名：
    <div class="layui-inline">
        <input class="layui-input" name="aname" id="aname" autocomplete="off">
    </div>
    手机号：
    <div class="layui-inline">
        <input class="layui-input" name="aphone" id="aphone" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>-->
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="barDemo">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="edit">恢复</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">弃用</a>
</script>

<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use(['table', 'layer', 'jquery'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;

        table.render({
            elem: '#test'
            , url: '/PUserQueryAll'
            , height: document.body.clientHeight - 180
            , page: true //开启分页
            , id: 'testReload'
            , cols: [[
                {field: "aid", title: "ID"},
                {field: "auserId", title: "用户编号" },
                {field: "roletype", title: "角色"},
                {field: "auser", title: "账号"},
                {field: "apassword", title: "密码"},
                {field: "aname", title: "姓名"},
                {field: "asex", title: "性别"},
                {field: "aphone", title: "手机号"},
                {field: "atime", title: "注册时间"
                    , templet: "<div>{{layui.util.toDateString(d.atime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                },
                {title: '操作', width: 250, toolbar: '#barDemo', align: 'center'}
            ]]
            , toolbar: '#toolbar',//表格头信息
            parseData: function (rs) {//数据格式解析
                console.log(rs);
                if (rs.code == 200) {
                    return {
                        "code": rs.code,
                        "msg": rs.msg,
                        "count": rs.data.total,
                        "data": rs.data.list
                    }
                }
            },
            response: {	// 设置响应码
                statusCode: 200 //规定成功的状态码，默认：0
            }
        });

        //监听工具条
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            /*if (obj.event === 'detail') {
                open("详情页面", "static/view/pseudoUser/pDetail.html?aid="+data.aid);/!*static/view/pseudoUser/!*!/
            } else */
                if (obj.event === 'del') {
                layer.confirm('真的弃用该用户吗？', function (index) {
                    $.ajax({
                        type: "POST",
                        url: "/UserAbandoned",
                        data: {aid: data.aid},
                        dataType: "json",
                        success: function (da) {
                            if (da.code == 200) {
                                layer.msg("弃用成功")
                                obj.del(); //删除对应行（tr）的DOM结构
                                location.reload();//刷新当前页面
                            } else {
                                layer.msg(da.msg)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }) //ajax结束
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                console.log(data);
                layer.confirm('真的恢复该用户吗？', function (index) {
                    $.ajax({
                        type: "POST",
                        url: "/UserRecover",
                        data: {aid: data.aid},
                        dataType: "json",
                        success: function (da) {
                            if (da.code == 200) {
                                layer.msg("恢复成功")
                                obj.del(); //删除对应行（tr）的DOM结构
                                location.reload();//刷新当前页面
                            } else {
                                layer.msg(da.msg)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }) //ajax结束
                    layer.close(index);
                });
            }
        });

        function searchBtn() {

        }

        /*var $ = layui.$, active = {
            reload: function () {
                var auserId = $('#auserId');
                // var aname = $('#aname');
                // var aphone = $('#aphone');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "auserId": auserId.val(),
                        // "aname": aname.val(),
                        // "aphone": aphone.val()
                    }
                });
            }
        };*/
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>
</html>